﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;
using GestionPedago.EntityContext.EntityOPTools;
using Persistence.OPManager;

namespace GestionPedago.EntityContext
{
   public partial class Formation : EntityObject
    {

       public List<CompetenceParActivite> GetListeCompetenceAPasser()
       {
           List<CompetenceParActivite> lstCompetencesTitre = new List<CompetenceParActivite>();
           foreach (ActiviteParTitre a in TitreOPManager.GetTitreByCode(this.CodeTitre).ActiviteParTitre)
           {
               lstCompetencesTitre.AddRange(a.GetActivite().CompetenceParActivite);
           }
           return lstCompetencesTitre;
       }
       
       
       public List<ECF> GetListeECFAPasser()
        {
             try
             {
                 List<CompetenceParActivite> lstCompetencesTitre = GetListeCompetenceAPasser();

               /* IQueryable<ECF> query = EntityConnection.GetContext().ECF;
                query.Where(it => ((EntityCollection<Competence>)it.Competence.Intersect(lstCompetencesTitre)).Count() > 0);
                query.Select(it => it);*/

                List<ECF> lst = new List<ECF>();
                List<ECF> listeECF = EntityConnection.GetContext().ECF.ToList();
                foreach (ECF e in listeECF)
                {
                    bool competenceEnCommun = false;

                    foreach(CompetenceParECF cpe in e.CompetenceParECF) {
                        CompetenceParActivite cpa = new CompetenceParActivite();
                        cpa.IdCompetence = cpe.Competence.IdCompetence;
                        if(lstCompetencesTitre.Contains(cpa, new CompetenceParActiviteComparerOnCompetence())) {
                            competenceEnCommun = true;
                        }
                    }
                    if(competenceEnCommun) {
                        lst.Add(e);
                    }
                }

                /*foreach (ECF e in query)
                {

                    lst.Add(e);
                }*/

                return lst;

             }
             catch (Exception ex)
             {
                 throw ex;
             }
            
        }
    }
}
